package com.zzj.portalwebsite.utlis;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * shopping
 * com.java2405.shopping.utils
 * 连接数据库
 *

 */
public class ConnectionDatabaseForMySQLUseJdbc {
    // 数据库连接的驱动名称
    private static String DRIVER = "com.mysql.cj.jdbc.Driver";
    // 数据库连接的地址URL
    private static String URL = "jdbc:mysql://localhost:3306/portalwebsite?serverTimezone=Asia/Shanghai";
    // 数据库的用户名
    private static String USERNAME = "root";
    // 数据库的密码
    private static String PASSWORD = "root";

    static Connection connection = null;


    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭数据库连接
     * @param connection
     * @param preparedStatement
     */
    public static void closeConnection(Connection connection, PreparedStatement preparedStatement) {
        try {
            if (null != preparedStatement) {
                preparedStatement.close();
            }
            if (null != connection) {
                connection.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("=======>"+(getConnection() == null));
    }
}
